iT邦幫忙

2023 iThome 鐵人賽

DAY 15
0
Software Development

當rust 遇上 cqrs & es系列 第 15

D15 測一下surrealDB

  • 分享至 

  • xImage
  •  

昨天在EventRepository加了surrealDB的實現,測一下EventStore是否也正確運作:

建立surrealDB (in memory)

let db = Surreal::new::<Mem>(()).await?;
db.use_ns("test").use_db("test").await?;
let repo = SurrealEventRepository { db: db.clone() };

依 cqrs framework建立reader的 cqrs操作接口

let reader_service = ReaderService {};
let reader_store: PersistedEventStore<SurrealEventRepository, Reader> = PersistedEventStore::new_event_store(repo.clone());
let reader_cqrs = CqrsFramework::new(reader_store, vec![], reader_service);

執行2位讀者建檔及借出一本書的操作

let _ = reader_cqrs.execute("test-reader-1", ReaderCommand::CreateReader {
    name: "John Smith".to_string(),
}).await;
let _ = reader_cqrs.execute("test-reader-2", ReaderCommand::CreateReader {
    name: "John Smith".to_string(),
}).await;
let _ = reader_cqrs.execute("test-reader-1", ReaderCommand::BorrowBook {
    book_id: "test-book-1".to_string(),
    borrowed_date: Utc::now(),
    due_date: Utc::now().add(chrono::Duration::days(7)),
}).await;

查看資料庫裡的events

let events : Vec<SerializedEvent> = repo.db.query("SELECT * FROM events").await?.take(0)?;
println!("======== Events ========\n{:#?}", events);
======== Events ========
[
    SerializedEvent {
        aggregate_id: "test-reader-1",
        sequence: 2,
        aggregate_type: "Reader",
        event_type: "ReaderEvent",
        event_version: "0.1.0",
        payload: Object {
            "BookBorrowed": Object {
                "book_id": String("test-book-1"),
                "borrowed_date": String("2023-09-30T12:40:37.344309166Z"),
                "due_date": String("2023-10-07T12:40:37.344312320Z"),
                "return_date": Null,
            },
        },
        metadata: Object {},
    },
    SerializedEvent {
        aggregate_id: "test-reader-2",
        sequence: 1,
        aggregate_type: "Reader",
        event_type: "ReaderEvent",
        event_version: "0.1.0",
        payload: Object {
            "ReaderCreated": Object {
                "id": String("4587683f-1181-4cf4-910d-282255424e7f"),
                "name": String("John Smith"),
            },
        },
        metadata: Object {},
    },
    SerializedEvent {
        aggregate_id: "test-reader-1",
        sequence: 1,
        aggregate_type: "Reader",
        event_type: "ReaderEvent",
        event_version: "0.1.0",
        payload: Object {
            "ReaderCreated": Object {
                "id": String("8e861d30-2702-4307-ab91-3a5e26e60adc"),
                "name": String("John Smith"),
            },
        },
        metadata: Object {},
    },
]

使用event store讀取 aggregate:

let reader_store: PersistedEventStore<SurrealEventRepository, Reader> = PersistedEventStore::new_event_store(repo.clone());
let reader = reader_store.load_aggregate("test-reader-1").await.unwrap();
println!("======== Reader ========\n{:#?}", reader.aggregate);
======== Reader ========
Reader {
    id: "8e861d30-2702-4307-ab91-3a5e26e60adc",
    name: "John Smith",
    borrowed_books: [
        BorrowedBook {
            book_id: "test-book-1",
            borrowed_date: 2023-09-30T12:40:37.344309166Z,
            due_date: 2023-10-07T12:40:37.344312320Z,
            return_date: None,
        },
    ],
    borrowed_history: [],
}

上一篇
D14 加persist層
下一篇
D16 Book ES in Surreal
系列文
當rust 遇上 cqrs & es30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言